package com.lanou.bbs.service.impl;

import com.lanou.bbs.dao.UserDao;
import com.lanou.bbs.entity.User;
import com.lanou.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public boolean canRegister(User user) {
        Date dt = new Date();
        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
        String format = dFormat.format(dt);
        user.setGender("男").setState(0).setRegDate(format);

        User user1 = userDao.canRegister(user);
        if (user1 != null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public User login(User user) {
        //调用dao 通过username 查询数据库
        User u = userDao.findByName(user.getName());

        if (u != null && u.getPassword().equals(user.getPassword())) {
            //登录成功

            return u;
        } else {
            //登录失败
            return null;
        }
    }

    @Override
    public User findById(int uid) {
        User user = userDao.findById(uid);
        user.setPassword("").setName("");
        return user;
    }
}
